package guoan.repository;

import java.util.List;

import guoan.domain.ExamItem;

import org.joda.time.DateTime;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ExamItemRepository extends PagingAndSortingRepository<ExamItem, Long>,
		JpaSpecificationExecutor<ExamItem> {

	Long countByExamIdAndOpenHoursAndStudentOpenid(Long examId, Long openHours, String studentOpenid);

	Long countByExamIdAndStudentOpenid(Long examId, String studentOpenid);

	List<ExamItem> findByStudentOpenid(String openid);

	@Modifying
	@Query(value = "DELETE FROM exam_item WHERE exam_id = ?", nativeQuery = true)
	void deleteByExamId(Long id);

	Long countByExamId(Long id);

	@Modifying
	@Query(value = "DELETE FROM exam_item WHERE date(exam_time) < date(now())", nativeQuery = true)
	void removeLast();

}
